import os
from tkinter import Tk, Label, Entry, ttk, Button, StringVar, Frame, PhotoImage, messagebox
from urllib.request import urlretrieve
import requests
from PIL import Image, ImageTk

def get_sign():
    #获取名字输入
    name = name_entry.get()
    #名字输入为空弹出消息框
    if name == "":
        messagebox.showinfo("提示", "请输入姓名。。。")
    #获取字体列表选择
    font = combox_list.get()
    #字体列表
    map_list = {"艺术签": "901","商务签": "904","一笔签": "905","正楷签": "2","可爱签":"3","行楷签":"6"}
    #网站地址
    url = 'http://www.yishuzi.com/b/re13.php'
    #网页请求(网页地址，数据)
    d = requests.post(url, data={'id': name, 'id1': map_list[font], 'id2': '#FFFFFF', 'id4': '#000000', 'id6': '#000000'})
    #解码，切片
    myurl = d.content.decode("utf-8").split('"')[1]

    #返回当前工作目录
    now = os.getcwd()
    #将目录和文件名合成一个路径
    now = os.path.join(now,'自定义签名')
    #目录不存在则创建目录
    if not os.path.exists(now):
        os.mkdir(now)
    #切换到新创建的路径
    os.chdir(now)
    #文件名
    file_name = name + '.jpg'
    #将远程数据下载到本地，即把签名下载到本地
    urlretrieve(myurl,file_name)
    #打开签名图片
    img = Image.open(file_name)
    photo = ImageTk.PhotoImage(img)
    #显示签名
    pic_label.config(image=photo)
    pic_label.image=photo

#创建窗口
root = Tk()
#标题
root.title("个性签名")
#logo
root.iconbitmap('logo.ico')
#窗口大小、位置
root.geometry('800x600+380+100')
#设置不可改变窗口大小
root.resizable(False, False)

#背景图片
bg_image = PhotoImage(file = 'bg.png')
#使用标签放置背景图片
bg_label = Label(root, image = bg_image)
#背景图片摆放
bg_label.place(relwidth = 1, relheight = 1)

#上面边框
frame = Frame(root, bg = '#edcc79', bd = 5)
frame.place(relx = 0.5, rely = 0.1, relwidth = 0.75, relheight = 0.1,anchor = 'n')
#输入签名
font_label = Label(frame, text = '输入签名:', font=('华文行楷',20),fg='red')
font_label.place(relwidth = 0.25, relheight = 1)
#输入框
name_entry = Entry(frame, font = ('微软雅黑', 15))
name_entry.place(relx = 0.25, relwidth = 0.36, relheight = 1)
#选择列表
combox_list = ttk.Combobox(frame, font=('华文行楷',20),state='readonly')
combox_list.place(relx = 0.63, relwidth = 0.2, relheight = 1)
combox_list["value"] = ("艺术签", "商务签", "一笔签","可爱签","正楷签","行楷签")
combox_list.current(0)#默认选择第一个
#按键
button = Button(frame, text = '生成', font = ('华文行楷', 20),fg='red', command =get_sign)
button.place(relx = 0.85, relwidth = 0.15, relheight = 1)
#下面边框
lower_frame = Frame(root, bg = '#edcc79', bd = 10)
lower_frame.place(relx = 0.5, rely = 0.25, relwidth = 0.75, relheight = 0.6, anchor = 'n')
pic_label = Label(lower_frame)
pic_label.place(relwidth = 1, relheight = 1)
#显示窗口
root.mainloop()


